﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;


public partial class mobile_hg : System.Web.UI.Page
{
    public string openid;
    public string enterid;
    public string productid;
    protected void Page_Load(object sender, EventArgs e)
    {
        openid = this.Request.Params["openid"].ToString();
        enterid = this.Request.Params["enterid"].ToString();
        productid = this.Request.Params["pid"].ToString();
        SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc.Open();
        SqlCommand get_name = new SqlCommand();
        get_name.CommandText="select PRODUCT_NAME from INFO_PRODUCT where PRODUCT_ID=@id";
        get_name.Connection = sc;
        get_name.Parameters.AddWithValue("@id",productid);
        object a=get_name.ExecuteScalar();
        if (a == null)//避免出错
        {
            sc.Close();
            Button1.Visible = false;
            Page.RegisterStartupScript("ss", "<script>alert('系统出错!')</script>");
            return;
        }
        else
        {//获取页面所需信息
            string name = a.ToString();
            title.Text = name;
            SqlCommand get_img = new SqlCommand();
            get_img.CommandText = "select PRODUCT_IMAGE from INFO_PRODUCT where PRODUCT_ID=@idd";
            get_img.Connection = sc;
            get_img.Parameters.AddWithValue("@idd",productid);
            img1.ImageUrl = get_img.ExecuteScalar().ToString();
            SqlCommand get_intro = new SqlCommand();
            get_intro.CommandText = "select PRODUCT_INTRO from INFO_PRODUCT where PRODUCT_ID=@iddd";
            get_intro.Connection = sc;
            get_intro.Parameters.AddWithValue("@iddd",productid);
            textbox1.Text = get_intro.ExecuteScalar().ToString();
            //获取商品所剩数目
            SqlCommand get_sum = new SqlCommand();
            get_sum.CommandText = "select EX_SUM from INFO_EXCHANGE where PRODUCT_ID=@idddd";
            get_sum.Connection = sc;
            get_sum.Parameters.AddWithValue("@idddd",productid);
            sum.Text = get_sum.ExecuteScalar().ToString() + "件等待您换购";
            sc.Close();
           
             
        }
        
    } //设置提交事件
    protected void Button1_Click(object sender, EventArgs e)
    {
        //先确认用户尚未换购过该产品
        SqlConnection sc1 = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
        sc1.Open();
        SqlCommand check_hg = new SqlCommand();
        check_hg.Connection = sc1;
        check_hg.CommandText = "select PRODUCT_ID from EXCHANGE_RECORD where PRODUCT_ID=@pid and IND_ID=(select IND_ID from IND_CARE where IND_OPENID=@oid and ENTER_ID=@eid)";
        check_hg.Parameters.AddWithValue("@pid",productid);
        check_hg.Parameters.AddWithValue("@oid",openid);
        check_hg.Parameters.AddWithValue("@eid",enterid);
        if (check_hg.ExecuteScalar() != null)//若换购过
        {
            sc1.Close();
            Page.RegisterStartupScript("ss", "<script>alert('您已经换购过了，请将机会留给其他人吧~!')</script>");
            return;
        }
        //未换购过
        //先验证sum是否为零，若为零跳出，不为零继续
        //再验证积分用户积分数是否大于所需积分数，EX_RATE，若大于等于则继续，小于跳出
        
        SqlCommand check_sum = new SqlCommand();
        check_sum.CommandText = "select EX_SUM from INFO_EXCHANGE where PRODUCT_ID=@idddd";
        check_sum.Connection = sc1;
        check_sum.Parameters.AddWithValue("@idddd", productid);
        if (Convert.ToInt32(check_sum.ExecuteScalar().ToString()) > 0)
        {
            SqlCommand get_rate = new SqlCommand();
            get_rate.CommandText = "select EX_RATE from INFO_EXCHANGE where PRODUCT_ID=@id";
            get_rate.Connection = sc1;
            get_rate.Parameters.AddWithValue("@id",productid);
            int rate =Convert.ToInt32( get_rate.ExecuteScalar().ToString());

            SqlCommand get_cre = new SqlCommand();
            get_cre.CommandText = "select IND_CREDITS from INFO_IND where IND_ID=(select IND_ID from IND_CARE where IND_OPENID=@oid and ENTER_ID=@eid)";
            get_cre.Connection = sc1;
            get_cre.Parameters.AddWithValue("@oid",openid);
            get_cre.Parameters.AddWithValue("@eid", enterid);
            int cred = Convert.ToInt32(get_cre.ExecuteScalar().ToString());

            if (cred >= rate)
            {
                string url = "mobile_hgconfirm.aspx?pid=" + productid + "&openid=" + openid + "&enterid=" + enterid;
                Response.Redirect(url);
            }
            else
            {
                sc1.Close();
                Page.RegisterStartupScript("ss", "<script>alert('您的积分不够哦!')</script>");
                return;
            }

        }
        else
        {
            sc1.Close();
            Page.RegisterStartupScript("ss", "<script>alert('对不起您下手晚了一步已经被人换购啦!')</script>");
            return;
        }
    }
}